package com.ytktt.study.week7.task6;

import com.ytktt.study.week7.task6.seq.dbms.DatabaseConnection;
import com.ytktt.study.week7.task6.seq.repository.SequenceInfoRepository;
import com.ytktt.study.week7.task6.snowflake.Snowflake;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * TODO
 *
 * @author ytk
 */
@Slf4j
public class Application {

    public static void main(String[] args) throws SQLException {
        genSnowflakeInOneSecond();
        genMockSeqInOneSecond();
    }

    private static void genSnowflakeInOneSecond() {
        Snowflake snowflake = new Snowflake(1, 1);
        long start = System.currentTimeMillis();
        int i = 0;
        for (; System.currentTimeMillis() - start < 1000; ++i) {
            snowflake.nextId();
        }
        log.info("snowflake count: {}", i);
    }

    private static void genMockSeqInOneSecond() throws SQLException {
        DatabaseConnection dc = DatabaseConnection.getInstance();
        SequenceInfoRepository repository = SequenceInfoRepository.getInstance();
        long start = System.currentTimeMillis();
        int i = 0;
        for (; System.currentTimeMillis() - start < 1000; ++i) {
            Connection connection = dc.getConnection();
            repository.genId("order_info", connection);
            connection.commit();
        }
        log.info("mock seq count: {}", i);
    }
}
